#include "domains/IAxisAlignedDomain.hpp"
#include "loaders/AxisAlignedLoader.hpp"

#include <cstddef>	// to get NULL
#include <math.h>	// to get fmin and fmax

AxisAlignedLoader::AxisAlignedLoader(IAxisAlignedDistribution *_sdist, IAxisAlignedDistribution *_vdist) {
	sdist=_sdist;
	vdist=_vdist;
	//IDistribution *_spatialDist, IDistribution *_velocityDist, double _density, double _np2c) {
/*	spatialDist=dynamic_cast<IAxisAlignedDistribution*>(_spatialDist);	// make sure spatialDist is axis aligned
	velocityDist=dynamic_cast<IAxisAlignedDistribution*>(_velocityDist);	// make sure velocityDist is axis aligned
	density=_density;
	np2c=_np2c;
*/
}

AxisAlignedLoader::~AxisAlignedLoader() {
}

void AxisAlignedLoader::setDomain(IAxisAlignedDomain *_dom) {
	dom=_dom;
	if (dom==NULL || sdist==NULL)
		return;
	xmin=fmax(dom->get_axis1_min(),sdist->get_axis1_min());
	ymin=fmax(dom->get_axis2_min(),sdist->get_axis2_min());
	xmax=fmin(dom->get_axis1_max(),sdist->get_axis1_max());
	ymax=fmin(dom->get_axis2_max(),sdist->get_axis2_max());	
}

/*void AxisAlignedParticleLoader::load(IParticlePopulation *pop) {
	IAxisAlignedDomain *aadom=dynamic_cast<IAxisAlignedDomain*>(dom);	// make sure domain is a subclass of IAxisAlignedDomain; this should throw a compile-time error if untrue
	//
	xmin=fmax(spatialDist->get_axis1_min(),aadom->get_axis1_min());
	xmax=fmin(spatialDist->get_axis1_max(),aadom->get_axis1_max());
	ymin=fmax(spatialDist->get_axis2_min(),aadom->get_axis2_min());
	ymax=fmin(spatialDist->get_axis2_max(),aadom->get_axis2_max());
	//
	double x,y,u,v;
	spatialDist->sample(x,y,randunity(),randunity());	// get first spatial sampling to enable first domain comparison in loop
	Vector pos(x,y);
	//
	unsigned cnt=0;
	while (cnt<nptcl) {
		while (!dom->contains(pos)) {
			spatialDist->sample(x,y,randunity(),randunity());
			pos.x=x;
			pos.y=y;
		}
		velocityDist->sample(u,v,randunity(),randunity());
		//
		Particle *p=new Particle();
		p->x=x;
		p->y=y;
		p->z=0;
		p->u=u;
		p->v=v;
		p->w=0;
		pop->add(p);
		//
		cnt++;
	}
}*/

